const { default: mongoose ,Schema} = require("mongoose")

mongoose.connect('mongodb://127.0.0.1:27017/bilibili')

mongoose.connection.once('open',async () => { 
    console.log('连接成功！\n ---------------------------------------------')
    const bookSchema = new Schema({
        name:String,
        author:String,
        price:Number,
        is_hot:Boolean
    })

    const bookModel = mongoose.model('novel',bookSchema)

    // bookModel.find()
    // .then((data) => { 
    //     console.log('查询数据库：',data)
    // }).catch((error) => { 
    //     console.log('查询失败！') 
    // })
    // bookModel.find({author:'余华'}).select({name:1,author:1,_id:0,is_hot:1})
    // .then((data) => { 
    //     console.log('查询数据库：',data)
    // }).catch((error) => { 
    //     console.log('查询失败！') 
    // })
    // bookModel.updateMany({author:'余华'},{is_hot:false}).select({name:1,author:1,_id:0,is_hot:1})
    // .then((data) => { 
    //     console.log('查询数据库：',data)
    // }).catch((error) => { 
    //     console.log('查询失败！') 
    // })
    // bookModel.find({$or:[{author:'余华'},{author:'罗贯中'}]}).select({name:1,author:1,_id:0})
    // .then((data) => { 
    //     console.log('查询数据库：',data)
    // }).catch((error) => { 
    //     console.log('查询失败！') 
    // })
    // bookModel.find({$and:[{price:{$gt:10}},{price:{$lt:20}}]}).select({name:1,author:1,_id:0,price:1})
    // .then((data) => { 
    //     console.log('查询数据库：',data)
    // }).catch((error) => { 
    //     console.log('查询失败！') 
    // })
    // bookModel.find().sort({price:1}).select({name:1,author:1,_id:0,price:1})
    // .then((data) => { 
    //     console.log('查询数据库：',data)
    // }).catch((error) => { 
    //     console.log('查询失败！') 
    // })
    // bookModel.find().sort({price:1}).skip(3).limit(2).select({name:1,author:1,_id:0,price:1})
    // .then((data) => { 
    //     console.log('查询数据库：',data)
    // }).catch((error) => { 
    //     console.log('查询失败！') 
    // })
})
mongoose.connection.on('error',() => { 
    console.log('连接失败！')
})
mongoose.connection.on('close',() => { 
    console.log('连接关闭！')
})